<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Action_Grademagica extends Controller{
    public function before(){
        if(!Model_User::logged_in(Kohana::$config->load("grademagica")->webuser_role_name)) {
            $redirect = '';
//                die($this->request->uri());
//                die('!'.$this->request->uri().'!');
            if($this->request->uri()!='/' && $this->request->uri()!='cadastro'){
                $redirect = '?redirect='.$this->request->uri();
            }
//                $this->request->redirect();
            $this->request->redirect($this->language."cadastro".$redirect);
        }
    }
    
    public function action_save_institution(){
        $user_id = Model_User::get_user()->get_id();
        $user_has_institution=Model_Institution::get_user_institution($user_id);
        if($user_has_institution){
            $institution = new Model_Institution($user_has_institution);
        }else{
            $institution = new Model_Institution();
            
        }
        $institution->set_university_name($_POST['university_name']);
        $institution->set_institute_name($_POST['institute_name']);
        $institution->set_city($_POST['institute_city']);
        $institution->set_state($_POST['institute_state']);
        $institution->set_user_id($user_id);
        $institution->save();
        
        Model_User::get_user()->set_last_modification();
        $institution_view = Smarty_View::factory("2_insert_data/existant_institution.tpl");
        $institution_view->institution_infos = $institution;
        die($institution_view->render());
    }
    public function action_save_course(){
        $user_id = Model_User::get_user()->get_id();
        $university_id = Model_Institution::get_user_institution($user_id);
        $course = new Model_Course();
        $course->set_code($_POST['course_code']);
        $course->set_created_in(date("Y-m-d H:i:s"));
        $course->set_credits($_POST['course_credits']);
        $course->set_institution_id($university_id);
        $course->set_title($_POST['course_title']);
        $course->set_user_id($user_id);
        $course->save();
        $course = $course->as_array();
        
        $course_view = Smarty_View::factory("2_insert_data/existant_course.tpl");
        $course_view->course = $course;
        $course_view->course['add_new_class_view'] = Smarty_View::factory("2_insert_data/class_form.tpl");
        $course_view->course['add_new_class_view']->course_id = $course['id'];
        $course_view->course['add_new_class_view']->render();
        foreach($course['classes'] as $j=>$class){
            $existant_class = Smarty_View::factory("2_insert_data/existant_class.tpl");
            $existant_class->class = $class;
            $existant_class->render();
            $course['classes'][$j] = $existant_class;
        }
        
        die($course_view);
    }
     public function action_save_class(){
//            print_r($_POST);die();
            
            $user_id = Model_User::get_user()->get_id();
            $course = new Model_Course($_POST['course_id']);
            if($course->get_user_id() != $user_id)
                die(__("Error linking this user with the given course"));
            
            $university_id = Model_Institution::get_user_institution($user_id);
            $class = new Model_Class();
            $class->set_course_id($_POST['course_id']);
            $class->set_identification($_POST['class_identification']);
            $class->set_priority($_POST['class_priority']);
            $class->set_teacher($_POST['class_teacher']);
            $class->save();
            
            //save class times
            $i=0;
            while(isset($_POST['class_day'.$i]) && isset($_POST['class_start'.$i]) && isset($_POST['class_end'.$i])){
                $class->set_class_time($_POST['class_day'.$i], $_POST['class_start'.$i], $_POST['class_end'.$i]);
                $i++;
            }
            if(isset($_POST['class_day']) && isset($_POST['class_start']) && isset($_POST['class_end'])){
                $class->set_class_time($_POST['class_day'], $_POST['class_start'], $_POST['class_end']);
            }
            
            Model_User::get_user()->set_last_modification();
//            $class->class_hours = Model_Class::get_class_times($class->get_id());
            $existant_class = Smarty_View::factory("2_insert_data/existant_class.tpl");
            $existant_class->class = $class;
            die($existant_class->render());
        }
    public function action_edit_institution(){
            $university = new Model_Institution();
            $university->set_university_name($_POST['university_name']);
            $university->set_institute_name($_POST['institute_name']);
            $university->set_city($_POST['institute_city']);
            $university->set_state($_POST['institute_state']);
            $university->set_user_id(Model_User::get_user()->get_id());
            $university->save();
            
            die("Universidade:".$university->get_university_name()."<br/>Instituto:".$university->get_institute_name()
                    ."<br/>Local:".$university->get_city()."/".$university->get_state());
    }
    public function action_edit_course(){
        $course = new Model_Course($_POST['course_id']);
        $user_id = Model_User::get_user()->get_id();
        if($course->get_user_id() != $user_id)
            die(__("Error linking this user with the given course"));
        $course->set_code($_POST['course_code']);
        $course->set_created_in(date("Y-m-d H:i:s"));
        $course->set_credits($_POST['course_credits']);
        $course->set_title($_POST['course_title']);
        $course->save();
        
        //build tpl structure
        $course=$course->as_array();
        $course_view = Smarty_View::factory("2_insert_data/existant_course.tpl");
        $course_view->course = $course;
        $course_view->editing_course = true;
        $course_view->course['add_new_class_view'] = Smarty_View::factory("2_insert_data/class_form.tpl");
        $course_view->course['add_new_class_view']->course_id = $course['id'];
        $course_view->course['add_new_class_view']->render();
        foreach($course['classes'] as $j=>$class){
            $existant_class = Smarty_View::factory("2_insert_data/existant_class.tpl");
            $existant_class->class = $class;
            $existant_class->render();
            $course_view->course['classes'][$j] = $existant_class;
        }
        
        //build JSON for replacing jquery ui accordion item
        $return = explode("##EDIT##CLASS##", $course_view->render());
        $return['title'] = $return[0];
        unset($return[0]);
        $return['content'] = $return[1];
        unset($return[1]);
        $return['course_id'] = $course['id'];
        
        die(json_encode($return));
    }
     public function action_edit_class(){
        $user_id = Model_User::get_user()->get_id();
        $class = new Model_Class($_POST['class_id']);
        $course = new Model_Course($class->get_course_id());
        if($course->get_user_id() != $user_id)
            die(__("Error linking this user with the given course"));
        
        $class->set_identification($_POST['class_identification']);
        $class->set_priority($_POST['class_priority']);
        $class->set_teacher($_POST['class_teacher']);
        $class->save();

        //save class times
        $class->remove_all_class_times();
        $i=0;
        while(isset($_POST['class_day'.$i]) && isset($_POST['class_start'.$i]) && isset($_POST['class_end'.$i])){
            $class->set_class_time($_POST['class_day'.$i], $_POST['class_start'.$i], $_POST['class_end'.$i]);
            $i++;
        }
        if(isset($_POST['class_day']) && isset($_POST['class_start']) && isset($_POST['class_end'])){
            $class->set_class_time($_POST['class_day'], $_POST['class_start'], $_POST['class_end']);
        }

        DB::update("user_infos")->set(array("last_modification"=>DB::expr("NOW()")))->where("user_id","=",$user_id)->execute();
        Model_User::get_user()->set_last_modification();
//            $class->class_hours = Model_Class::get_class_times($class->get_id());
        $existant_class = Smarty_View::factory("2_insert_data/existant_class.tpl");
        $existant_class->class = $class;
        die($existant_class->render());
     }
     public function action_load_class(){
        $user_id = Model_User::get_user()->get_id();
        $class = new Model_Class($_POST['class_id']);
        $course = new Model_Course($class->get_course_id());
        if($course->get_user_id() != $user_id)
            die(__("Error linking this user with the given course"));
        
        $existant_class = Smarty_View::factory("2_insert_data/existant_class.tpl");
        $existant_class->class = $class;
        die($existant_class->render());
     }
     public function action_remove_course(){
         $user_id = Model_User::get_user()->get_id();
         $course = new Model_Course($_POST['course_id']);
         if($course->get_user_id() != $user_id)
            die(__("Error linking this user with the given course"));
         else{
             $course->remove();
         }
         Model_User::get_user()->set_last_modification();
         die("ok");
     }
     public function action_remove_class(){
         $user_id = Model_User::get_user()->get_id();
         $class = new Model_Class($_POST['class_id']);
         $course = new Model_Course($class->get_course_id());
         if($course->get_user_id() != $user_id)
            die(__("Error linking this user with the given course"));
         else
             $class->remove();
         Model_User::get_user()->set_last_modification();
         die("ok");
     }
     public function action_edit_class_interface(){
        $class = new Model_Class($_POST['class_id']);
        $course = new Model_Course($class->get_course_id());
        if($course->get_user_id() != Model_User::get_user()->get_id())
            die(__("Error linking this user with the given class"));
        else{
            $existant_class = Smarty_View::factory("2_insert_data/class_form.tpl");
            $existant_class->class_for_editing = $class;                
            die($existant_class->render());
        }
    }
     public function action_edit_course_interface(){
        $course = new Model_Course($_POST['course_id']);
        if($course->get_user_id() != Model_User::get_user()->get_id())
            die(__("Error linking this user with the given class"));
        else{
            $existant_course = Smarty_View::factory("2_insert_data/course_form.tpl");
            $existant_course->course_for_editing = $course;                
            die($existant_course->render());
        }
    }
    public function action_edit_institution_interface(){
        $user_id = Model_User::get_user()->get_id();
        $institution=Model_Institution::get_user_institution($user_id,false);
//        print_r($institution);die();
        if($institution){
            die(Smarty_View::factory("2_insert_data/institution_form.tpl")
                    ->set("institution",$institution)->render());
        }

    }
}